package edu.hcmus.sow.dao;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.hcmus.sow.domain.FileAccessRight;

/**
 * DAO to manage FileAccessRight entities.
 * 
 */
@Repository
@Transactional
public class FileAccessRightDAOImpl extends AbstractHibernateDAO<FileAccessRight, Integer> implements
      FileAccessRightDAO {
   public List<FileAccessRight> getByFileID(Integer fileID) throws DataAccessException {
      String queryString = "select fac from FileAccessRight fac join fetch fac.file f join fetch fac.user u "
            + "where fac.file.id = ?";

      List<FileAccessRight> list = executeQuery(queryString, fileID);
      return list;
   }

   public FileAccessRight findByFileAndUser(Integer fileID, Integer userID) throws DataAccessException {
      List<FileAccessRight> list = findByCriteria(Restrictions.eq("fileID", fileID), Restrictions.eq("userID", userID));
      return list.size() == 0 ? null : list.get(0);
   }
}
